package admin

import (
	"fmt"
	"strings"
	"tcsc/models"
	"time"

	"github.com/astaxie/beego"
)

type IndexBlock struct {
	BaseController
}

func (c *IndexBlock) Get() {
	gid := c.Gid
	if real := models.Query(`select * from mt_index_block where gid=` + gid); real == nil {
		now := time.Now().Local().Format("2006-01-02 15:04:05")
		err := models.Exec(`insert into mt_index_block(gid,name,addtime,sort,status,img,intr,pnum,defaultsort,mobileimg,pcImgSize,mobileImgSize) 
			select ` + gid + `,name,'` + now + `',sort,'0',img,intr,pnum,defaultsort,mobileimg,pcImgSize,mobileImgSize from mt_index_block where gid = 0`)
		if err != nil {
			beego.Debug(err)
			c.Ctx.Redirect(302, "/admin/index/index")
			return
		}
	}
	page, page2, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `, ` + num
	ob := ` order by a.defaultsort, a.addtime desc`
	sql := `select a.*, b.pathname from mt_index_block a left join mt_resources b on a.img = b.id where a.gid=` + gid + ob + lt
	sql2 := `select count(*) as totals from mt_index_block where gid=` + gid
	info := models.Query(sql)
	c.Data["gid"] = gid
	c.Data["info"] = info
	c.Data["paginator"] = models.Page(sql2, page2, num)
	c.Data["url"] = `/admin/indexBlock/index?`
	c.TplName = "admin/indexBlock/index.html"
}

func (c *IndexBlock) Post() {
	js := make(map[string]interface{})
	c.Data["json"] = &js
	id := c.GetString("id")
	table := c.GetString("table")
	err := models.Exec(`update ` + table + ` set status=abs(status-1) where id=` + id)
	if err != nil {
		beego.Debug("更改状态"+table+"失败:", err)
		js["status"] = 0
	} else {
		js["status"] = 1
	}
	c.ServeJSON()
}

type IndexBlockAE struct {
	BaseController
}

func (c *IndexBlockAE) Get() {
	gid := c.Gid
	condition := ""
	if gid == "0" {
		condition = "join (select * from mt_shop where style=0 and status=1 and censoring=1) b on a.gid=b.id where"
	} else {
		condition = "where a.gid=" + gid + " and"
	}
	column := models.Query(`select id, name from mt_column where type='p' and status=1 and pid=0`)
	for _, v := range column {
		var cids []string
		cids = append(cids, fmt.Sprint(v["id"]))
		son := models.Query(`select id from mt_column where pid=` + v["id"].(string))
		for _, s1 := range son {
			cids = append(cids, fmt.Sprint(s1["id"]))
			son2 := models.Query(`select id from mt_column where pid=` + s1["id"].(string))
			for _, s2 := range son2 {
				cids = append(cids, fmt.Sprint(s2["id"]))
			}
			s1["son"] = son2
		}
		cidss := strings.Join(cids, ",")
		v["product"] = models.Query(`
			select a.id,a.title from mt_product a ` + condition + ` a.status=1 and a.censoring=1 and a.cid in(` + cidss + `) order by a.cid`)
	}
	c.Data["column"] = column
	c.Data["gid"] = gid
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select a.*, b.pathname from mt_index_block a left join mt_resources b on a.img=b.id where a.id=` + id); info != nil {
			mobileimg := models.Query(`select b.pathname as mobileimg from mt_index_block a left join mt_resources b on a.mobileimg=b.id where a.id=` + id)[0]["mobileimg"]
			c.Data["mobileimg"] = mobileimg
			c.Data["info"] = info[0]
		} else {
			c.Ctx.Redirect(302, "/admin/indexBlock/index")
		}
	}
	c.TplName = "admin/indexBlock/ae.html"
}

func (c *IndexBlockAE) Post() {
	defer c.ServeJSON()
	input := c.Input()
	if c.Gid == "0" {
		name := input.Get("name")
		intr := input.Get("intr")
		img := input.Get("img")
		mobileimg := input.Get("mobileimg")
		defaultsort := input.Get("defaultsort")
		err := models.Exec(`update mt_index_block set name='` + name + `',intr='` + intr + `',img='` + img + `',mobileimg='` + mobileimg + `' where gid <> 0 and defaultsort=` + defaultsort)
		if err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
			return
		}
	}
	input.Set("cid", strings.Replace(strings.Trim(strings.Join(c.GetStrings("cid"), ","), ","), ",,", ",", -1))
	input.Set("pid", strings.Replace(strings.Trim(strings.Join(c.GetStrings("pid"), ","), ","), ",,", ",", -1))
	if _, err := models.Save("mt_index_block", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
}
